package com.inews.business.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.utils.Identities;
import com.inews.business.entity.Brand;
import com.inews.business.repository.BrandDao;

@Service
@Transactional(readOnly=true)
public class BrandServiceImpl implements BrandService{
	
	@Autowired
	private BrandDao brandDao;
	
	
	/**
	 * 分页查询产品类别.
	 *
	 * @param filterParams the filter params
	 * @param pageNumber the page number
	 * @param pageSize the page size
	 * @param sortType the sort type
	 * @return the product type
	 */
	public Page<Brand> getBrand(Map<String, Object> filterParams, int pageNumber, int pageSize,
			String sortType) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("visable", new SearchFilter("visible", Operator.EQ, true));
		Specification<Brand> spec = DynamicSpecifications.bySearchFilter(filters.values(), Brand.class);
		PageRequest pageRequest = new PageRequest(pageNumber-1, pageSize, new Sort(Direction.DESC, "name"));
		return brandDao.findAll(spec, pageRequest);
	}
	
	
	/**
	 * 保存品牌.
	 *
	 * @param brand the brand
	 */
	@Transactional(readOnly=false)
	public void saveBrand(Brand brand){
		brand.setCode(Identities.uuid2());
		brandDao.save(brand);
	}
	
	
	

}
